package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.picture.lib.entity.LocalMedia;
import com.ruihang.generalibrary.ui.adapter.BaseEditHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.ui.adapter.holder.RebllionCommitFootHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.RebllionCommitHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class RebllionCommitAdapter extends BaseEditHolderAbsAdapter<LocalMedia, RebllionCommitHolder> {
    private List<LocalMedia> list = null;
    private List<LocalMedia> selectors;
    private int clickedPos;//点击了并跳转了的pos
    private OnItemInternalClick onLongItemClick;
    private RemainListSize remainListSize;

    public RemainListSize getRemainListSize() {
        return remainListSize;
    }

    public RebllionCommitAdapter setRemainListSize(RemainListSize remainListSize) {
        this.remainListSize = remainListSize;
        return this;
    }

    public OnItemInternalClick getOnLongItemClick() {
        return onLongItemClick;
    }

    public RebllionCommitAdapter setOnLongItemClick(OnItemInternalClick onLongItemClick) {
        this.onLongItemClick = onLongItemClick;
        return this;
    }

    public RebllionCommitAdapter(Context context, List<LocalMedia> list) {
        super(context, list);
        this.list = list;
        selectors = new ArrayList<>();
    }

    @Override
    public void onItemInternalClick(View itemView, View view, int position) {
        if (position == data.size()) {
            clickedPos = position;
            super.onItemInternalClick(itemView, view, position);
        } else {
            if (isEdting) {
                selectors.add(data.remove(position));
                if(remainListSize!=null){
                    remainListSize.getRemainListSize(data);
                }
                notifyItemRemoved(position);
            } else {
                clickedPos = position;
                super.onItemInternalClick(itemView, view, clickedPos);
            }
        }

    }

    @Override
    public synchronized void setEdting(boolean edting) {
        super.setEdting(edting);
        if (edting) {
            selectors.clear();
        }
    }

    @Override
    public int getSelectorCount() {
        return selectors.size();
    }

    @Override
    public List<LocalMedia> getSelectList() {
        return selectors;
    }

    @Override
    public RebllionCommitHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_rebllion_com_pic, parent, false);
            RebllionCommitHolder holder = new RebllionCommitHolder(context, view);
            holder.setInternalClick(this);
            holder.setOnItemInternalClick(onLongItemClick);
            return holder;
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_rebllion_com_foot, parent, false);
            RebllionCommitFootHolder holder = new RebllionCommitFootHolder(context, view);
            holder.setInternalClick(this);
            return holder;
        }
    }

    @Override
    public int getItemCount() {
        if (data != null) {
            int total = data.size();
            if (total > 0) {
                return total + 1;
            }
        }
        return 1;
    }

    @Override
    public LocalMedia getItem(int position) {
        if (position == getDataSize()) {
            return new LocalMedia(true);
        }
        return super.getItem(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getDataSize()) {
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public RebllionCommitAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    public interface RemainListSize{
        void getRemainListSize(List<LocalMedia> list);
    }

    public List<LocalMedia> getEqualDatas(){
        return data;
    }
}
